# -*- coding: utf-8 -*-
# @Time    : 2021/11/18 14:32
# @Author  : travel2040
# @Email   : travel2040@163.com
# @File    : prometheus-client-监测机器指标.py

# 需要安装的库
# pip install psutil== 5.2.2
# pip install prometheus-client==0.6.0


import time
import psutil
from threading import Thread
from prometheus_client import start_http_server,Gauge


def send():
    machine_name = "machine"
    remark = "检测机器指标"

    g_ps = Gauge(machine_name, remark,
                 labelnames=["machine_name", "category"])

    g_ps_label_mem_percent = g_ps.labels(machine_name=machine_name, category="mem_percent")
    g_ps_label_cpu_percent = g_ps.labels(machine_name=machine_name, category="cpu_percent")
    # g_ps_label_mem_used = g_ps.labels(machine_name="public_service", category="mem_used")
    # g_ps_label_mem_total = g_ps.labels(machine_name="public_service", category="mem_total")

    while True:

        try:
            cpu_percent = psutil.cpu_percent(1)
            phymem = psutil.virtual_memory()
            mem_percent = phymem.percent
            # mem_used = int(phymem.used / 1024 / 1024)
            # mem_total = int(phymem.total / 1024 / 1024)

            g_ps_label_cpu_percent.set(cpu_percent)
            g_ps_label_mem_percent.set(mem_percent)
            # g_ps_label_mem_used.set(mem_used)
            # g_ps_label_mem_total.set(mem_total)


        except Exception as e:
            pass

        time.sleep(10)


if __name__ == '__main__':
    t = Thread(target=send)
    t.start()

    start_http_server(8081)



